from timeit import Timer

import sys
import math


def Problem():
    """If the numbers 1 to 5 are written out in words: 
    one, two, three, four, five, 
    then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

    If all the numbers from 1 to 1000 (one thousand) inclusive were 
    written out in words, how many letters would be used?

    NOTE: Do not count spaces or hyphens. For example, 
    342 (three hundred and forty-two) contains 23 letters and 115 
    (one hundred and fifteen) contains 20 letters. The use of "and"
    when writing out numbers is in compliance with British usage.
    """     
    
#===============================================================================
#    single = ["one", "two", "three", "four", "five", 
#              "six", "seven", "eight", "nine"]
#    teens = ["eleven", "twelve" ,"thirteen", "fourteen", "fifteen",
#            "sixteen", "seventeen", "eighteen", "nineteen"]
#    tens = ["twenty", "thirty", "forty", "fifty", 
#            "sixty", "seventy", "eighty", "ninety"]
#    
#    
#    single_len = sum(map(len, single))
#    teens_len = sum(map(len, teens))
#    tens_len = sum(map(len, tens))
#    
#    print single_len, teens_len, tens_len
#    
#    #ones, teens
#    len19 = single_len + teens_len + len("ten")
#    #1..19 + 20..29 + 30..39 + ..
#    len99 = tens_len + 9*(tens_len + single_len) + len19
#    #1..99 + 100..199 + 200..99 + ...
#    len999 = (989*single_len + 989*len("hundred") + 989*len("and") + 9*len99) + (single_len + len("hundred")) + len99
#    len1000 = len(single[1]) + len("thousand") + len999
#    
#    print len99, len999
#    
#    ans = len1000
#===============================================================================
    
    single = ["one", "two", "three", "four", "five", 
              "six", "seven", "eight", "nine"]
    teens = ["ten", "eleven", "twelve" ,"thirteen", "fourteen", "fifteen",
            "sixteen", "seventeen", "eighteen", "nineteen"]
    tens = ["twenty", "thirty", "forty", "fifty", 
            "sixty", "seventy", "eighty", "ninety"]    
    
    tween = single + teens
    
    def num_word_hundreds(x):
        result = ""
        
        quotient, remainder = divmod(x, 100)
        deca = num_word_deca(remainder)
        
        if quotient:
            result = single[quotient - 1] + " hundred"
        
        if quotient and deca:
            result = result + " and " + deca
        elif deca:
            result = deca
            
        return result
            
    def num_word_deca(x):
        result = ""
        
        if x == 0:
            result = ""
        elif x < 20:
            result = tween[x - 1]
        else:
            quotient, remainder = divmod(x, 10)
            if remainder:
                result = tens[quotient - 2] + "-" + single[remainder - 1]
            else:
                result = tens[quotient - 2]
        
        return result 


    letters = lambda x: len(filter(lambda y: str(y).isalpha(), x))

    nums = map(num_word_hundreds, xrange(1, 1000)) + ["one thousand"]
    result = map(letters,nums)
        
    ans = sum(result)
    print "Answer for Problem 17 = %s " % (ans,)


    #debug
    print map(num_word_hundreds, [3, 30, 330, 333, 300])

    
if __name__ == "__main__":
    t = Timer(setup='from __main__ import Problem', stmt='Problem()').timeit(1)
    print "Execution time = %0.3f seconds" %(t,)